package net.chenyan.server.controller.admapi;/**
 * @author chenyan
 * @createTime 2022/5/13 11:06
 * @description description
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenyan.commons.core.R;
import net.chenyan.commons.util.JWTUtil;
import net.chenyan.server.config.AppConfig;
import net.chenyan.server.dto.UserAddDTO;
import net.chenyan.server.dto.UserLoginDTO;
import net.chenyan.server.po.User;
import net.chenyan.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.chenyan.commons.util.JWTUtil;

import java.util.HashMap;
import java.util.Map;

/**
 *@description: TODO
 *@author: chenyan
 *@create: 2022/5/13 11:06
 */
@Slf4j
@Api(tags = {"用户注册登录接口"})
@RestController
@RequestMapping(value = "/admapi/userLogin")
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private AppConfig appConfig;


    @ApiOperation(value = "登录操作")
    @PostMapping("/login")
    public R login(@Validated @RequestBody UserLoginDTO userLoginDTO) {

        if (userLoginDTO.getTel() == null) {
            return R.error("请输入电话号");
        }
        if (userLoginDTO.getPassword() == null) {
            return R.error("请输入密码");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel", userLoginDTO.getTel());
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            return R.error("用户不存在，请注册！");
        }
        if (user.getPassword().equals(userLoginDTO.getPassword()) ==false) {
            return R.error("密码错误，请重新登录");
        }
        //使用用户身份生成jwt token，发送给浏览器
        Map<String,Object> claims = new HashMap<>();
        claims.put("userId",user.getId());
        String token= JWTUtil.createJWT(claims, appConfig.getJwtSubject(),appConfig.getJwtSecret(),appConfig.getJwtLife());
        Map<String,Object> map = new HashMap<>();
        map.put("user",user);
        map.put("token",token);
        return R.data(map);
    }


    @ApiOperation(value = "用户注册")
    @PostMapping("/add")
    public R add(@Validated @RequestBody UserAddDTO userAddDTO) {
        System.out.println(userAddDTO);
        if (userAddDTO.getTel() == null) {
            return R.error("电话号不能为空!");
        }
        if(userAddDTO.getPassword() == null){
            return R.error("密码不能为空!");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel", userAddDTO.getTel());
        User user1 = userService.getOne(queryWrapper);
        if (user1 != null) {
            return R.error("该电话号已注册，请登录！");
        }
        User user2 = userAddDTO.toUser();
        boolean temp = userService.save(user2);
        return temp ? R.ok("注册成功,请登录！") : R.error("注册失败");
    }

}
